<?php namespace Admin\Controller; 

use Common\Model\GoodsList;
//货品控制器
class Goods_listController extends CommonController{

//  模型句柄
	private $ldb;
	public function __init(){
		$this->ldb=new GoodsList;
		parent::__init();
	}
	public function index(){
  
//		获取要添加商品的id
		$gid=Q('get.gid',0,'intval');
//		获取商品的货品列表要添加的规格数据
 //		获取组
		$attrData=Db::table('goods_attr ga')
		->join('shop_type_attr ta','ga.taid','=','ta.taid')
		->where('ga.gid',$gid)
		->where('ta.class',1)
		->orderBy('ga.gtid')
		->groupBy('ta.taid')
		->get();
//		p($attrData);die;
//		获取所属的规格数据，压入子数组中
		foreach($attrData as $k=>$v){
		 $attrData[$k]['sonData']=Db::table('goods_attr ga')
		->join('shop_type_attr ta','ga.taid','=','ta.taid')
		->where('ga.gid',$gid)
		->where('ta.class',1)
		->where('ta.taid',$v['taid'])
		->get();
		}
//		p($attrData);die;
        View::with('attrData',$attrData);
		$data=$this->ldb->where('gid',$gid)->get();
//		把combine字段拆分
		foreach($data as $k=>$v){
//			重组组合属性
			$data[$k]['combine']=explode(',',$v['combine']);
		}
//		p($data);die;
		foreach($data as $k=>$v){
			foreach($v['combine'] as $kk=>$vv){
				$data[$k]['combine'][$kk]=Db::table('goods_attr')->where('gtid',$vv)->pluck('gtvalue');
			}
		}
//		p($data);die;
		View::with('gid',$gid);		
		View::with('data',$data);
		View::make();
	}
//	货品添加
	public function add(){
     $gid=Q('post.gid',0,'intval');
	  if(IS_POST){
//	  	p($_POST);die;
       $result=$this->ldb->store();
	   if(!$result){
			View::error($this->ldb->getError());
		}
	   View::success('添加成功',U('index',array('gid'=>$gid)));
	  } 
	
	}
//	ajax添加货品验证
	public function check(){
		$gid=Q('get.gid',0,'intval');
		$combine=Q('get.combine',0,'intval');
//		p($combine);die;
		$listModel=new \Common\Model\GoodsList;
		echo $listModel->where('gid',$gid)->where('combine',implode(',', $combine))->count()? 0 : 1;
		
	}
//	货品编辑
	public function edit(){
		$gid=Q('get.gid',0,'intval');
		$glid=Q('get.glid',0,'intval');
		if(IS_POST){
//			p($_POST);die;
        $result=$this->ldb->edit();
	   	if(!$result){
			View::error($this->ldb->getError());
		}
		View::success('编辑成功',U('index',array('gid'=>$gid)));
	} 
		
//		获取该商品下的货物
		$listData=$this->ldb->where('gid',$gid)->get();
		foreach($listData as $k=>$v){
				$listData[$k]['combine']=explode(',', $v['combine']);
			}
//		获取属性
		$atModel=new \Common\Model\goods_attr;
		foreach($listData as $k=>$v){
			foreach($listData[$k]['combine'] as $kk=>$vv){
				$listData[$k]['combine'][$kk]=$atModel->where('gtid',$vv)->pluck('gtvalue');
			} 
		}
		View::with('listData', $listData);
//		获取原来的数据
		$oldData=$this->ldb->where('glid',$glid)->find();
        $oldData['combine']=explode(',', $oldData['combine']);
        View::with('oldData', $oldData);
//		类型属性表关联
        $data=Db::table('goods_attr ga')->join('shop_type_attr ta','ga.taid','=','ta.taid')->where('gid',$gid)->where('class',1)->get();
        View::with('gid', $gid);
        View::with('glid', $glid);
		View::with('data', $data);
		View::make();
	}
//   货品删除
     public function del(){
       $glid=Q('get.glid',0,'intval');
	   $gid=Q('get.gid',0,'intval');
	   $this->ldb->where('glid',$glid)->delete();
	   View::success('删除成功',U('index',array('gid'=>$gid)));
      }
}